package com.wxx.interpret;

import java.util.Stack;

/**
 * @author Jetwang
 * @version 1.0
 * @date 2022/5/27 20:35
 */
public class InterpretDemo01 {
    /**
     * 此模式略
     */
    public static void main(String[] args) {
        String str = "20 4 12 3 + * -";
        String[] s = str.split(" ");
        int res = 0;
        Stack stack = new Stack();
        for (String s1 : s) {
            if (s1.matches("\\d+")) {
                stack.push(Integer.valueOf(s1));
            } else {
                Integer num01 = (Integer) stack.pop();
                Integer num02 = (Integer) stack.pop();
                if (s1.equals("+")) {
                    res = num01 + num02;
                    stack.push(res);
                } else if (s1.equals("*")) {
                    res = num01 * num02;
                    stack.push(res);
                } else {
                    res = num01 - num02;
                    stack.push(res);
                }
            }
        }
        System.out.println("res = " + res);
    }
}
